package cn.zzuisa.controller;
import cn.zzuisa.base.R;
import cn.zzuisa.entity.Diss;
import cn.zzuisa.service.DissService;
import cn.zzuisa.service.InvitationService;
import cn.zzuisa.service.MemberService;
import cn.zzuisa.service.ModuleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.Api;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "三方网站数据接口")
@RestController
@RequestMapping("/other")
public class OtherController {

    @ApiOperation("首页展示")
    @GetMapping("/list")
    public R<List<Map<String,String>>> list() {
        try {
            // http://www.zgcdiy.com/ajax_content.php?ajax_act=index_diy_type_list?filter=
            Document doc = Jsoup.connect("https://pro.jd.com/mall/active/2VyRHGE7jM1igBJcrjoB6ak1JJWV/index.html?cpdad=1DLSUE")
                    .timeout(50000).get();
            Elements select = doc.select(".pd_common_wrap");
            Element first = select.get(1);
            Elements a = first.select("a");
            List<Map<String,String>> list = new ArrayList<>();
            a.forEach(aa -> {
                Map<String,String> map = new HashMap<>();
                String src = aa.attr("href");
                map.put("hrefto",src);
                String src1 = aa.select(".pd_pic_content img").get(0).attr("data-src");
                map.put("img","http:" + src1);
                String title = aa.select(".pd_title").text();
                map.put("title",title);
                String price = aa.select(".pd_now_price").text();
                map.put("price",price);
                list.add(map);
            });
            return R.ok(list);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  null;
    }

    @Autowired
    private MemberService memberService;

    @Autowired
    private InvitationService invitationService;

    @Autowired
    private ModuleService moduleService;

    @Autowired
    private DissService dissService;


    @ApiOperation("统计数据")
    @GetMapping("show_data")
    public R<Map> dataCensus() {
        int memberCount = memberService.count(new QueryWrapper<>());
        int invitationCount = invitationService.count(new QueryWrapper<>());
        int moduleCount = moduleService.count(new QueryWrapper<>());
        int unDoCount = dissService.count(new QueryWrapper<Diss>().eq("status", 1));
        Map<String,Integer> map = new HashMap<>();
        map.put("memberCount",memberCount);
        map.put("invitationCount",invitationCount);
        map.put("moduleCount",moduleCount);
        map.put("unDoCount",unDoCount);
        return R.ok(map);
    }

}
